#define _CRT_SECURE_NO_WARNINGS 
#include"clist.h"
CL* CLBuyNode(int x)
{
	CL* New = (CL*)malloc(sizeof(CL));
	if (New == NULL)
	{
		perror("malloc fail");
		return NULL;
	}
	New->next = NULL;
	New->data = x;
	return New;
}
void CLPrint(CL* phead)
{
	CL* cur = phead;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

void CLPushBack(CL** pphead, int x)
{
	assert(pphead);
	CL* New = CLBuyNode(x);
	if (*pphead == NULL)
	{
		*pphead = New;
	}

	else
	{
		CL* cur = *pphead;
		while (cur->next)
		{
			cur = cur->next;
		}

		cur->next = New;
	}
}


void CLPushFront(CL** pphead, int x)
{
	assert(pphead);
	CL* New = CLBuyNode(x);
	New->next = *pphead;
	*pphead = New;
}

void CLPopBack(CL** pphead)
{
	assert(pphead);
	assert(*pphead);
	CL* cur = *pphead;
	if (cur->next == NULL)
	{
		free(cur);
		cur = NULL;
		*pphead = NULL;
		return;
	}
	while (cur->next->next)
	{
		cur = cur->next;
	}
	free(cur->next);
	cur->next = NULL;
}
void CLPopFront(CL** pphead)
{
	assert(pphead);
	assert(*pphead);
	CL* cur = *pphead;
	*pphead = cur->next;
	free(cur);
	cur = NULL;
}
CL* CLFind(CL* phead, int x)
{
	CL* cur = phead;
	while (cur)
	{
		if (cur->data == x)
			return cur;

		cur = cur->next;
	}
	return cur;
}
void CLInsert(CL** pphead, CL* pos, int x)
{
	assert(pos);
	assert(pphead);
	CL* New = CLBuyNode(x);
	New->next = pos;
	CL* cur = *pphead;
	if (pos == cur)
	{
		*pphead = New;
		return;
	}
	while (cur->next != pos)
	{
		cur = cur->next;
	}
	cur->next = New;
}
void CLErase(CL** pphead, CL* pos)
{
	assert(pos);
	assert(pphead);
	CL* cur = *pphead;
	if (pos == *pphead)
	{
		*pphead = cur->next;
		free(cur);
		return;
	}
	while (cur->next != pos)
	{
		cur = cur->next;
	}
	cur->next = pos->next;
	free(pos);
}
void CLInsertAfter(CL* pos, int x)
{
	assert(pos);
	CL* New = CLBuyNode(x);
	New->next = pos->next;
	pos->next = New;
}
void CLEraseAfter(CL* pos)
{
	assert(pos);
	assert(pos->next);
	CL* del = pos->next;
	pos->next = pos->next->next;
	free(del);
	del == NULL;
}